package abq2.jic.jic.plugin.operate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.gpt.IGPTAction;


import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * 用于查询书籍信息的插件
 */
public class DemoTask implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String , String> result = new HashMap<>();
        //如果操作名称相等
        if ("GET_Library_Books".equalsIgnoreCase(action)) {
            //获取书籍名称
            String name = params.get("name");
            //获取DynamicObject列表，将要获取的字段信息注入
            DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_books_info",
                    "number," +
                            "abq2_textfield," +
                            "name," +
                            "group," +
                            "abq2_introduction",
                    (new QFilter("name", QCP.equals, name)).toArray());
            //创建一个JsonArray
            JSONArray jsonArray = new JSONArray();
            for (DynamicObject dynamicObject : dys) {
                //将每一个书籍的信息加入JSONArray
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("number", dynamicObject.getString("number"));
                jsonObject.put("abq2_textfield", dynamicObject.getString("abq2_textfield"));
                jsonObject.put("name", dynamicObject.getString("name"));
                jsonObject.put("group", dynamicObject.getString("group"));
                jsonObject.put("abq2_introduction", dynamicObject.getString("abq2_introduction"));
                jsonArray.add(jsonObject);
            }
            System.out.println(jsonArray.toJSONString());
            //加入resultDynamicObject参数，将JsonArray加入到这个参数当中，然后返回
            result.put("resultDynamicObject", jsonArray.toJSONString());
        }
        return result;
    }
}